package com.xbfinance.manager.service.impl;

import com.xbfinance.manager.dao.P2pUserMapper;
import com.xbfinance.manager.dao.P2pUserinfoMapper;
import com.xbfinance.manager.dao.UserCustomMapper;
import com.xbfinance.manager.pojo.po.P2pUser;
import com.xbfinance.manager.pojo.po.P2pUserExample;
import com.xbfinance.manager.pojo.po.P2pUserinfo;
import com.xbfinance.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserCustomMapper userDao;
    @Autowired
    private P2pUserinfoMapper userinfoDao;
    @Override
    public boolean isNameExist(String username) {
        P2pUserExample example=new P2pUserExample();
        example.createCriteria()
                .andUsernameEqualTo(username);
        List<P2pUser> userList = userDao.selectByExample(example);
        if(userList!=null&&userList.size()!=0)
            return true;
        return false;
    }

    @Override
    public boolean isEmailExist(String email) {
        P2pUserExample example=new P2pUserExample();
        example.createCriteria()
                .andEmailEqualTo(email);
        List<P2pUser> userList = userDao.selectByExample(example);
        if(userList!=null&&userList.size()!=0)
            return true;
        return false;
    }

    @Override
    public P2pUser regist(P2pUser p2pUser) {
        int rows = userDao.insertSelective(p2pUser);
        P2pUserExample example=new P2pUserExample();
        example.createCriteria()
                .andUsernameEqualTo(p2pUser.getUsername());
        if(rows!=0){
            return userDao.selectByExample(example).get(0);
        }
        return null;
    }

    @Override
    public boolean putInfo(String realname, String cardnum, Integer userid) {
        P2pUser p2pUser=new P2pUser();
        p2pUser.setUserid(userid);
        p2pUser.setRealname(realname);
        P2pUserinfo userinfo=new P2pUserinfo();
        userinfo.setUserid(userid);
        userinfo.setCardnum(cardnum);
        boolean flag=false;
        try {
            userDao.updateByPrimaryKeySelective(p2pUser);
            userinfoDao.insertSelective(userinfo);
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
